﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Specialized;
using Peer.Security;
namespace Peer.Web
{
    [Serializable]
    public class UrlString 
    {
        private NameValueCollection _nvc;
        public UrlString(NameValueCollection nvc)
        {
            _nvc = new NameValueCollection(nvc);
        }
        public UrlString()
        {
            _nvc = new NameValueCollection();
        }
        public virtual void Add(NameValueCollection c)
        {
            _nvc.Add(c);
        }
        public void Add(string name, string value)
        {
            _nvc.Add(name, value);
        }
        public void Remove(string name)
        {
            _nvc.Remove(name);
        }
        public void Set(string name, string value)
        {
            _nvc.Set(name, value);
        }
        public virtual string this[string name]
        {
            get
            {
                return _nvc[name];
            }
            set
            {
                _nvc[name]=value;
            }
        }
        public virtual string GetUrlString(string urlTarget)
        {
            bool isOriginal = !urlTarget.Contains("?");
            StringBuilder sb = new StringBuilder(urlTarget);
            foreach (string key in _nvc.Keys)
            {
                if (!string.IsNullOrEmpty(_nvc[key]))
                {
                    if (isOriginal)
                    {
                        sb.Append("?");
                        isOriginal = false;
                    }
                    else
                    {
                        sb.Append("&");
                    }
                    sb.Append(key);
                    sb.Append("=");
                    sb.Append(_nvc[key]);
                }
            }
            return sb.ToString();
        }
        public NameObjectCollectionBase.KeysCollection Keys
        {
            get
            {
                return _nvc.Keys;
            }
        }
    }
    [Serializable]
    public class SecurityUrlString:UrlString
    {
        const string BEGIN = "[";
        const string END = "]";
        public SecurityUrlString(NameValueCollection nvc)
        {
            Add(nvc);
        }
        public override void Add(NameValueCollection nvc)
        {
            string value;
            foreach (string key in nvc)
            {
                value = nvc[key];
                if (value.StartsWith(BEGIN) && value.EndsWith(END))
                {
                    value = PeerSecurity.DecryptString(value.Substring(1, value.Length - 2));
                }
                base[key] = value;
            }
        }

        public SecurityUrlString()
        {
        }
        public override string GetUrlString(string urlTarget)
        {
            bool isOriginal = !urlTarget.Contains("?");
            StringBuilder sb = new StringBuilder(urlTarget);
            foreach (string key in base.Keys)
            {
                if (!string.IsNullOrEmpty(base[key]))
                {
                    if (isOriginal)
                    {
                        sb.Append("?");
                        isOriginal = false;
                    }
                    else
                    {
                        sb.Append("&");
                    }
                    sb.Append(key);
                    sb.Append("=");
                    sb.Append(BEGIN);
                    sb.Append(PeerSecurity.EncryptString(base[key]));
                    sb.Append(END);
                }
            }
            return sb.ToString();
        }

    }
}
